package com.ruoyi.web.controller;


import com.ruoyi.web.pojo.R;
import com.ruoyi.utils.StringUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Getter;
import lombok.Setter;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "示例接口")
@RequestMapping("/test")
@RestController
public class TestController {
    private final static Map<Integer, UserEntity> users = new LinkedHashMap<Integer, UserEntity>();

    static {
        users.put(1, new UserEntity(1, "admin", "admin123", "15888888888"));
        users.put(2, new UserEntity(2, "ry", "admin123", "15666666666"));
    }


    @Operation(summary = "GET hello 接口 ")
    @Parameters({
            @Parameter(name = "id", description = "消息内容", required = true, in = ParameterIn.PATH),
            @Parameter(name = "token", description = "请求token", required = false, in = ParameterIn.HEADER),
            @Parameter(name = "msg", description = "文件名称", required = true, in = ParameterIn.QUERY)
    })
    @GetMapping("/hello/{id}")
    public String hello(@PathVariable("id") String id, @RequestHeader(name = "token", required = false) String token, @RequestParam("msg") String msg) {
        return "Hello~  " + id + "  " + token + "  " + msg;
    }


    @Operation(summary = "获取用户列表")
    @GetMapping("/list")
    public R<List<UserEntity>> userList() {
        List<UserEntity> userList = new ArrayList<UserEntity>(users.values());
        return R.ok(userList);
    }

    @Operation(summary = "获取用户详细")
    @Parameters(
            @Parameter(name = "userId", description = "用户ID", required = true, in = ParameterIn.PATH)
    )
    @GetMapping("/{userId}")
    public R<UserEntity> getUser(@PathVariable Integer userId) {
        if (!users.isEmpty() && users.containsKey(userId)) {
            return R.ok(users.get(userId));
        } else {
            return R.fail("用户不存在");
        }
    }

    @Operation(summary = "新增用户")
    @PostMapping("/save")
    public R<String> save(UserEntity user) {
        if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId())) {
            return R.fail("用户ID不能为空");
        }
        users.put(user.getUserId(), user);
        return R.ok();
    }

    @Operation(summary = "更新用户")
    @PutMapping("/update")
    public R<String> update(@RequestBody UserEntity user) {
        if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId())) {
            return R.fail("用户ID不能为空");
        }
        if (users.isEmpty() || !users.containsKey(user.getUserId())) {
            return R.fail("用户不存在");
        }
        users.remove(user.getUserId());
        users.put(user.getUserId(), user);
        return R.ok();
    }

    @Operation(summary = "删除用户信息")
    @DeleteMapping("/{userId}")
    public R<String> delete(@PathVariable Integer userId) {
        if (!users.isEmpty() && users.containsKey(userId)) {
            users.remove(userId);
            return R.ok();
        } else {
            return R.fail("用户不存在");
        }
    }

    @Getter
    @Setter
    @ApiModel(value = "UserEntity", description = "用户实体")
    private static class UserEntity implements Serializable {
        @ApiModelProperty("用户ID")
        private Integer userId;

        @ApiModelProperty("用户名称")
        private String username;

        @ApiModelProperty("用户密码")
        private String password;

        @ApiModelProperty("用户手机")
        private String mobile;

        public UserEntity() {

        }

        public UserEntity(Integer userId, String username, String password, String mobile) {
            this.userId = userId;
            this.username = username;
            this.password = password;
            this.mobile = mobile;
        }
    }
}
